package com.wsj.dao.hibernate;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.dao.DataAccessException;
import org.springframework.orm.hibernate3.SessionFactoryUtils;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.wsj.dao.IHomeSiteAreaMessageDao;
import com.wsj.util.page.PageResultSet;
import com.wsj.util.page.PerfectPageResultSet;
import com.wsj.util.tool.Tool;
import com.wsj.web.subclass.displayBean.homeSiteAreaMessage.HomeSiteAreaMessageExtendBean;
import com.wsj.web.vo.homeSite.HomeSiteAreaMessage;

public class HomeSiteAreaMessageDaoImpl extends HibernateDaoSupport  implements IHomeSiteAreaMessageDao {

	public void add(HomeSiteAreaMessage homeSiteAreaMessage)
			throws DataAccessException {
		this.getHibernateTemplate().save(homeSiteAreaMessage);
	}

	public void delete(Integer id) throws DataAccessException {
		HomeSiteAreaMessage h=(HomeSiteAreaMessage) this.getHibernateTemplate().get(HomeSiteAreaMessage.class, id);
		this.getHibernateTemplate().delete(h);
	}

	public HomeSiteAreaMessage loadByID(Integer id) throws DataAccessException {
		// TODO Auto-generated method stub
		HomeSiteAreaMessage homeSiteAreaMessage=null;
		homeSiteAreaMessage=(HomeSiteAreaMessage) this.getHibernateTemplate().get(HomeSiteAreaMessage.class, id);
		return homeSiteAreaMessage;
	}

	public PageResultSet loadHomeSiteAreaMessageByExtendBean(HomeSiteAreaMessageExtendBean extBean)
			throws DataAccessException {
//		 初始化
		PageResultSet pageResultSet = new PageResultSet();

		PerfectPageResultSet pageResult = new PerfectPageResultSet();

		// 获取Session
		Session session = SessionFactoryUtils.getSession(getSessionFactory(),
				true);

		// 初始化SQL语句
		StringBuffer stringBuffer = new StringBuffer("from HomeSiteAreaMessage as m where 1=1 ");

		// 设置每页显示得记录数和开始页
		pageResult.setPage(extBean.getPage(), extBean.getPageSize());
		
        // 参数
        List<Comparable> list = new ArrayList<Comparable>();
        
        //标题
        if(extBean.getTitle()!= null&&!extBean.getTitle().equals("")){
            stringBuffer.append(" and m.title like ?");
            
            list.add("%"+extBean.getTitle()+"%");
        }

        //删除
        if(extBean.getIsDel()!= null&&extBean.getIsDel()!=-2){
        	stringBuffer.append(" and m.isDel = ?");
        	
        	list.add(extBean.getIsDel());
        }
        //审核
        if(extBean.getAudite()!= null&&extBean.getAudite()!=-2){
        	stringBuffer.append(" and m.audite = ?");
        	
        	list.add(extBean.getAudite());
        }
        //推荐
        if(extBean.getIsCommend()!= null && extBean.getIsCommend()!=-2){
        	stringBuffer.append(" and m.isCommend = ?");
        	
        	list.add(extBean.getIsCommend());
        }
        //类别
        if(extBean.getHomeSiteNewsClassId()!= null && extBean.getHomeSiteNewsClassId()!=-2){
        	stringBuffer.append(" and m.homeSiteNewsClassId = ?");
        	
        	list.add(extBean.getHomeSiteNewsClassId());
        }
		stringBuffer.append(" order by m.id desc ");
		
		 String[] strParams = Tool.toArray(list);
	        pageResultSet = pageResult.pageResultSet(stringBuffer.toString(),
	                session, strParams);

		return pageResultSet;
	}

	public void update(HomeSiteAreaMessage homeSiteAreaMessage)
			throws DataAccessException {
		this.getHibernateTemplate().update(homeSiteAreaMessage);
	}

	public List loadHomeSiteAreaMessageList(HomeSiteAreaMessageExtendBean areaMessExtend) throws DataAccessException {
		StringBuffer hql=new StringBuffer(" from HomeSiteAreaMessage as h where 1=1 ");
		
		//推荐
		if(areaMessExtend.getIsCommend()!=null)
		{
			hql.append(" and h.isCommend = ?");
			
		}
		//删除
		if(areaMessExtend.getIsDel()!=null)
		{
			hql.append(" and  h.isDel = ?");
		}
		//审核
		if(areaMessExtend.getAudite()!=null)
		{
			hql.append(" and  h.audite = ?");
		}
		//图片
		if(areaMessExtend.getIsImg()!=null)
		{
			hql.append(" and  h.isImg = ?");
			hql.append(" and  h.img != null and h.isOutLink =1 and h.outLink != null ");
		}
		//栏目
		if(areaMessExtend.getHomeSiteNewsClassId()!=null && areaMessExtend.getHomeSiteNewsClassId().intValue()!=0)
		{
			hql.append(" and  h.homeSiteNewsClassId = ?");
		}
		
		hql.append(" order by h.id desc");
		
		
		Query q=this.getSession().createQuery(hql.toString());
		Integer param=0;
//		推荐
		if(areaMessExtend.getIsCommend()!=null)
		{
			q.setParameter(param, areaMessExtend.getIsCommend());
			param = param +1;
		}
//		删除
		if(areaMessExtend.getIsDel()!=null)
		{
			q.setParameter(param, areaMessExtend.getIsDel());
			param = param +1;
		}
//		审核
		if(areaMessExtend.getAudite()!=null)
		{
			q.setParameter(param, areaMessExtend.getAudite());
			param = param +1;
		}
		//图片
		if(areaMessExtend.getIsImg()!=null)
		{
			q.setParameter(param, areaMessExtend.getIsImg());
			param = param +1;
		}
		//栏目
		if(areaMessExtend.getHomeSiteNewsClassId()!=null && areaMessExtend.getHomeSiteNewsClassId().intValue()!=0)
		{
			q.setParameter(param, areaMessExtend.getHomeSiteNewsClassId());
			param = param +1;
		}
		q.setFirstResult(0);
		q.setMaxResults(areaMessExtend.getPage());
		return q.list();
	}

}








